<?php
namespace App\Repositories;




use App\Models\Coach;
use App\Models\Comment;
use App\Models\Complaint;
use App\Models\Evaluate;
use App\Models\PutPeriod;
use App\Models\School;
use App\Models\Student;
use App\Models\StudentState;
use App\Models\Subscribe;
use App\Models\CoachLeave;
use App\Models\Teacharea;
use App\Models\Trainingcar;
use Carbon\Carbon;
use App\Repositories\SubscribesRepository;
use Illuminate\Support\Facades\DB;

class SchoolRepository extends BaseRepository
{
    public $error = '';
    protected  $school;
    public function __construct(
        School $school
    )
    {
        $this->model = $school;
    }

    /**
     * 驾校统计
     * @param array $school_info
     * @param string $extra
     * @param string $size
     */
    public function index($school_info, $extra='', $size='')
    {
        $data['stu_count'] = Student::where('school_id', $school_info->id)->count();
        $data['coach_count'] = Coach::where('school_id', $school_info->id)->count();
        $data['car_count'] = Trainingcar::where('school_id', $school_info->id)->count();
        $data['area_count'] = Teacharea::where('school_id', $school_info->id)->count();
        $data['period_count'] = PutPeriod::where('school_id', $school_info->id)->sum('sum_period');
        $data['subscribe_count'] = Subscribe::where('school_id', $school_info->id)->where('status','!=',3)->count();

        $coach_comment_total = Comment::leftJoin('coachs','coachs.id','=','comments.coach_id')
                                        ->where('coachs.school_id',$school_info->id)->count();
        $coach_comment_good = Comment::leftJoin('coachs','coachs.id','=','comments.coach_id')
                                        ->where('coachs.school_id',$school_info->id)
                                        ->where('comments.comment','>',0)
                                        ->count();
        $data['coach_good_percent'] = 0;
        if($coach_comment_total && $coach_comment_good){
            $data['coach_good_percent'] = number_format($coach_comment_good / $coach_comment_total,2);
        }

        return $data;
    }


    /**
     * 查询教练详情
     * @param $id
     * @return array
     */
    public function getByInfo($student_info,$id)
    {
        $coach = $this->model->where('id',$id)->select('id','school_id','name','coachnum','photo','sex','idcard','mobile','address','teachpermitted')->first();
        if($coach){
            $coach->evaluateNum = Comment::where('coach_id', $coach->id)->count();

            $evaluate_perfect = Comment::where('comment', 2)->where('coach_id', $coach->id)->count();
            $evaluate_good = Comment::where('comment', 1)->where('coach_id', $coach->id)->count();
            $evaluate_normal = Comment::where('comment', 0)->where('coach_id', $coach->id)->count();
            $coach->evaluationrate =   $coach->evaluateNum ? number_format(($evaluate_good + $evaluate_perfect) /  $coach->evaluateNum,2) : 0;


            $evaluate = Comment::where('coach_id', $coach->id)->take(3)->get()->toArray();
            foreach ($evaluate as $k=>$v){
                $student = Student::where('id',$v['student_id'])->first();
                $evaluate[$k]['stuname'] = $student?$student->name:'';
                $evaluate[$k]['stuphoto'] = $student?$student->photo:'';
            }
            $coach->evaluate = $evaluate;
            $startime = time();
            $endtime = strtotime(date('Y-m-d 23:59:59'));
            $putime = PutPeriod::where('school_id',$coach->school_id)->where('coach_id',$id)->whereBetween('startime',[$startime,$endtime])->get();

            if(count($putime)){
                foreach($putime as $put){
                    $put->student_list = Subscribe::leftJoin('students', 'students.id', '=', 'yy_subscribes.student_id')
                        ->select('students.name','students.photo','students.id')
                        ->where('yy_subscribes.status', '!=', '3')
                        ->where('yy_subscribes.putime_id', $put->id)->take(4)->get();
                    $put->price = $this->subscribe->getPrice($student_info, $put);
                    $put->ssms = $student_info->service->paymode;
                }
            }

            $coach->putime = $putime;
        }
        return $coach;

    }


    /**
     * 存储odb
     * @param $inputs
     */
    public function store($inputs, $extra = '')
    {

    }


    /**
     * 更新
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input,$extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$input);
    }


    /**
     * 编辑特定id资源
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {

    }

    /**
     * @param $model
     * @param $inputs
     * @param bool $falg
     * @return mixed
     */
    public function save($model , $inputs , $falg = false)
    {
        return $model;
    }


    /**
     * @param School $school
     * @param $date
     */
    public function subscribeList(School $school, $date)
    {
        $start_time = strtotime($date);
        $end_time = $start_time + 86399;

        $coach_list = PutPeriod::leftJoin('coachs', 'coachs.id', '=', 'yy_put_periods.coach_id')
                                ->select(DB::raw('sum(sum_period) as sum_period'), 'coachs.id as coach_id', 'coachs.name', 'coachs.photo')
                                ->whereBetween('startime', [$start_time, $end_time])
                                ->where('yy_put_periods.school_id', $school->id)
                                ->groupBy('coach_id')->get()->toArray();

        if($coach_list){

            foreach ($coach_list as $k => $coach){
                $coach_list[$k]['put_info'] = PutPeriod::whereBetween('startime', [$start_time, $end_time])
                                                ->where('coach_id', $coach['coach_id'])->limit(4)
                                                ->orderBy('startime')->get()->toArray();
            }

        }

        return $coach_list;
    }

    /**
     * 驾校报表
     */
    public function report(School $school, $start_time, $end_time)
    {
        //学员数
        $data['stu_count'] = Student::where('school_id', $school->id)
                            ->whereBetween('applydate', [date('Y-m-d', $start_time), date('Y-m-d', $end_time)])
                            ->count();
        //教练数
        $data['coach_count'] = Coach::where('school_id', $school->id)
                            ->whereBetween('created_at', [date('Y-m-d', $start_time), date('Y-m-d 23:59:59', $end_time)])
                            ->count();
        //车辆数
        $data['car_count'] = Trainingcar::where('school_id', $school->id)
                            ->whereBetween('created_at', [date('Y-m-d', $start_time), date('Y-m-d 23:59:59', $end_time)])
                            ->count();
        //场地数
        $data['area_count'] = Teacharea::where('school_id', $school->id)
                            ->whereBetween('created_at', [date('Y-m-d', $start_time), date('Y-m-d 23:59:59', $end_time)])
                            ->count();
        //工时投放数
        $data['putperiod_count'] = PutPeriod::where('school_id', $school->id)
                            ->whereBetween('startime', [$start_time, $end_time])
                            ->sum('sum_period');
        //已完成数
        $data['finish_count'] = Subscribe::leftJoin('yy_put_periods', 'yy_put_periods.id', '=', 'yy_subscribes.putime_id')
                            ->whereBetween('yy_put_periods.startime', [$start_time, $end_time])
                            ->where('yy_subscribes.status', 4)
                            ->where('yy_subscribes.school_id', $school->id)
                            ->sum('study_time');
        //学员评论数
        $data['comment_count'] = Comment::leftJoin('students', 'students.id', '=', 'comments.student_id')
                            ->whereBetween('comments.createtime', [$start_time, $end_time])
                            ->where('students.school_id', $school->id)
                            ->count();
        //学员投诉数
        $data['complain_count'] = StudentState::leftJoin('students', 'students.id', '=', 'student_state.student_id')
                            ->whereBetween('student_state.createtime', [$start_time, $end_time])
                            ->where('students.school_id', $school->id)
                            ->count();

        return $data;
    }
}